package com.epalmpay.service.app.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.push.model.v20160801.PushNoticeToAndroidRequest;
import com.aliyuncs.push.model.v20160801.PushNoticeToAndroidResponse;
import com.aliyuncs.push.model.v20160801.PushNoticeToiOSRequest;
import com.aliyuncs.push.model.v20160801.PushNoticeToiOSResponse;
import com.epalmpay.dto.machinewasher.MachineWasherOutDTO;
import com.epalmpay.dto.machinewasher.MachineWasherOutDTOList;
import com.epalmpay.dto.messsage.AppNoticeInDTO;
import com.epalmpay.dto.messsage.AppNoticeListOutDTO;
import com.epalmpay.dto.messsage.AppNoticeOutDTO;
import com.epalmpay.entity.Notice;
import com.epalmpay.entity.Page;
import com.epalmpay.mapper.NoticeMapper;
import com.epalmpay.service.app.IAppNoticeService;
import com.epalmpay.status.Rescode;
import com.epalmpay.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.Map;

/**
 * Created by Administrator on 2018/2/27.
 */
@Service("appNoticeService")
public class AppNoticeServiceImpl implements IAppNoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public void sendNotice(Notice notice) {
        noticeMapper.insert(notice);
    }

    @Override
    public void sendNoticeToUsers(Notice notice) throws Exception{
        PushNoticeToAndroidRequest androidRequest = new PushNoticeToAndroidRequest();
        //内容较大的请求，使用POST请求
        androidRequest.setMethod(MethodType.POST);
        androidRequest.setAppKey(Long.parseLong(Const.APPKEY));
        androidRequest.setTitle(notice.getNoticetitle());
        androidRequest.setBody(notice.getNoticebody());
        androidRequest.setAcceptFormat(FormatType.JSON);
        androidRequest.setExtParameters("{\"type\":\""+notice.getType()+"\",\"isplay\":\""+notice.getIsplay()+"\",\"tvalue\":\""+notice.getTvalue()+"\"}");

        PushNoticeToiOSRequest iosRequest = new PushNoticeToiOSRequest();
        //内容较大的请求，使用POST请求
        iosRequest.setMethod(MethodType.POST);
        iosRequest.setAppKey(Long.parseLong(Const.APPKEY));
        iosRequest.setTitle(notice.getNoticetitle());
        iosRequest.setBody(notice.getNoticebody());
        iosRequest.setAcceptFormat(FormatType.JSON);
//        iosRequest.setApnsEnv("PRODUCT");
        iosRequest.setApnsEnv(Const.APNSENV);
        iosRequest.setExtParameters("{\"type\":\""+notice.getType()+"\",\"isplay\":\""+notice.getIsplay()+"\",\"tvalue\":\""+notice.getTvalue()+"\"}");

        if("ACCOUNT".equals(notice.getNoticetarget())) {//根据账号发送推送
            androidRequest.setTarget("ACCOUNT");
            androidRequest.setTargetValue(notice.getReceiver());
            iosRequest.setTarget("ACCOUNT");
            iosRequest.setTargetValue(notice.getReceiver());
        } else if("ALL".equals(notice.getNoticetarget())) {//全部推送
            androidRequest.setTarget("ALL");
            androidRequest.setTargetValue("ALL");
            iosRequest.setTarget("ALL");
            iosRequest.setTargetValue("ALL");
        }

        IClientProfile profile = DefaultProfile.getProfile(Const.REGION, Const.ACCESSKEYID, Const.ACCESSKEYSECRET);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        PushNoticeToAndroidResponse pushNoticeToAndroidResponse = client.getAcsResponse(androidRequest);
        PushNoticeToiOSResponse pushNoticeToiOSResponse = client.getAcsResponse(iosRequest);

        noticeMapper.insert(notice);
    }

    @Override
    public AppNoticeListOutDTO queryAppNoticeList(AppNoticeInDTO appNoticeInDTO, Page page) {
        AppNoticeListOutDTO appNoticeListOutDTO = new AppNoticeListOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId",appNoticeInDTO.getGroupId());
        param.put("noticeStatus",appNoticeInDTO.getNoticeStatus());
        param.put("memberName",appNoticeInDTO.getMemberName());
        param.put("joinStart",appNoticeInDTO.getJoinStart());
        param.put("joinEnd",appNoticeInDTO.getJoinEnd());


        int sumCount = noticeMapper.queryAppNoticeCount(param);
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return appNoticeListOutDTO;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));

        List<AppNoticeOutDTO> appNoticeOutDTOList = noticeMapper.queryAppNoticeList(param);
        appNoticeListOutDTO.setAppNoticeOutDTOList(appNoticeOutDTOList);
        return appNoticeListOutDTO;
    }
}
